﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SharePointAccelerators.Common
{
    public enum CategoriesEnum
    {
        Acceleratio
    }

    /// <summary>
    /// Source: Logging to ULS in SharePoint 2010 by Waldek Mastykarz
    /// http://blog.mastykarz.nl/logging-uls-sharepoint-2010/
    /// </summary>
    public class LoggingService : SPDiagnosticsServiceBase
    {
        public static string AreaName = "Acceleratio";
        private static LoggingService _Current;
        public static LoggingService Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new LoggingService();
                }

                return _Current;
            }
        }

        private LoggingService()
            : base("Acceleratio Logging Service", SPFarm.Local)
        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
            {
                new SPDiagnosticsArea(AreaName, new List<SPDiagnosticsCategory>
                {
                    new SPDiagnosticsCategory(CategoriesEnum.Acceleratio.ToString(), TraceSeverity.Unexpected, EventSeverity.Error)
                })
            };

            return areas;
        }

        public static void LogException(CategoriesEnum Category, Exception ex)
        {
            LogError(Category, ex.Message + "\n\n" + ex.StackTrace);
        }

        public static void LogError(CategoriesEnum Category, string errorMessage)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[AreaName].Categories[Category.ToString()];
            LoggingService.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
        }
    }
}
